﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using VueShopApi.Common;
using VueShopApi.Entities;

namespace VueShopApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ReportsController : AuthorizeController
    {
        private readonly ILogger<ReportsController> _logger;
        private readonly IFreeSql _context;

        public ReportsController(ILogger<ReportsController> logger, IFreeSql context)
        {
            _logger = logger;
            _context = context;
        }

        [HttpGet("type/{typeId}")]
        public async Task<IActionResult> Get([FromRoute] int typeId)
        {
            JObject obj = typeId switch
            {
                1 => await reportOne(),
                2 => await reportTwo(),
                3 => await reportThree(),
                _ => null,
            };
            return obj == null ? ServerResponse.No($"未找到报表{typeId}对应的数据！") : ServerResponse.Yes(obj);
        }

        private async Task<JObject> reportOne()
        {
            var reports = await _context.Select<SpReport1>().ToListAsync();
            if (!reports.Any()) return null;
            var areaGroup = reports.GroupBy(r => r.Rp1Area);
            var dateGroup = reports.GroupBy(r => r.Rp1Date);
            var obj = JObject.Parse("{\"legend\":{\"data\":[]},\"yAxis\":[{\"type\":\"value\"}],\"xAxis\":[{\"data\":[]}],\"series\":[]}");
            obj["legend"]["data"] = JsonConvert.DeserializeObject<JArray>(JsonConvert.SerializeObject(areaGroup.Select(g => g.Key).ToArray()));
            // obj["yAxis"] = JsonConvert.DeserializeObject<JArray>(JsonConvert.SerializeObject(new[] { new { type = "value" } }));
            obj["xAxis"][0]["data"] = JsonConvert.DeserializeObject<JArray>(JsonConvert.SerializeObject(dateGroup.Select(g => g.Key?.ToString("yyyy-MM-dd")).ToArray()));
            obj["series"] = JsonConvert.DeserializeObject<JArray>(JsonConvert.SerializeObject(areaGroup.Select(g => new { name = g.Key, type = "line", statck = "总量", areaStyle = new { }, data = g.Select(s => s.Rp1UserCount).ToArray() })));
            return obj;
        }

        private async Task<JObject> reportTwo()
        {
            var reports = await _context.Select<SpReport2>().ToListAsync();
            if (!reports.Any()) return null;
            var pageGroup = reports.GroupBy(r => r.Rp2Page);
            var dateGroup = reports.GroupBy(r => r.Rp2Date);
            var obj = JObject.Parse("{\"legend\":{\"data\":[]},\"yAxis\":[{\"type\":\"value\"}],\"xAxis\":[{\"data\":[]}],\"series\":[]}");
            obj["legend"]["data"] = JsonConvert.DeserializeObject<JArray>(JsonConvert.SerializeObject(pageGroup.Select(g => g.Key).ToArray()));
            // obj["yAxis"] = JsonConvert.DeserializeObject<JArray>(JsonConvert.SerializeObject(new[] { new { type = "value" } }));
            obj["xAxis"][0]["data"] = JsonConvert.DeserializeObject<JArray>(JsonConvert.SerializeObject(dateGroup.Select(g => g.Key?.ToString("yyyy-MM-dd")).ToArray()));
            obj["series"] = JsonConvert.DeserializeObject<JArray>(JsonConvert.SerializeObject(pageGroup.Select(g => new { name = g.Key, type = "line", statck = "总量", areaStyle = new { }, data = g.Select(s => s.Rp2Count).ToArray() })));
            return obj;
        }

        private async Task<JObject> reportThree()
        {
            var reports = await _context.Select<SpReport3>().ToListAsync();
            if (!reports.Any()) return null;
            var srcGroup = reports.GroupBy(r => r.Rp3Src);
            var dateGroup = reports.GroupBy(r => r.Rp3Date);
            var obj = JObject.Parse("{\"legend\":{\"data\":[]},\"yAxis\":[{\"type\":\"value\"}],\"xAxis\":[{\"data\":[]}],\"series\":[]}");
            obj["legend"]["data"] = JsonConvert.DeserializeObject<JArray>(JsonConvert.SerializeObject(srcGroup.Select(g => g.Key).ToArray()));
            // obj["yAxis"] = JsonConvert.DeserializeObject<JArray>(JsonConvert.SerializeObject(new[] { new { type = "value" } }));
            obj["xAxis"][0]["data"] = JsonConvert.DeserializeObject<JArray>(JsonConvert.SerializeObject(dateGroup.Select(g => g.Key?.ToString("yyyy-MM-dd")).ToArray()));
            obj["series"] = JsonConvert.DeserializeObject<JArray>(JsonConvert.SerializeObject(srcGroup.Select(g => new { name = g.Key, type = "line", statck = "总量", areaStyle = new { }, data = g.Select(s => s.Rp3Count).ToArray() })));
            return obj;
        }
    }
}
